package com.gitee.l0km.thrift.swift2thrift.generator;

import com.facebook.swift.codec.ThriftField.Requiredness;
import com.gitee.l0km.codegen.annotations.CodegenRequired;
import com.gitee.l0km.xthrift.base.metadata.DecoratorThriftFieldMetadata;
import com.gitee.l0km.xthrift.base.metadata.ThriftFieldMetadataUtil;
import com.google.common.base.Function;

import static com.gitee.l0km.thrift.swift2thrift.generator.CodegenPreAllocFilter.PREALLOC_FILTER;

public class ErpcTypePrefixGetter implements Function<DecoratorThriftFieldMetadata, String> {

	public ErpcTypePrefixGetter() {
	}

	@Override
	public String apply(DecoratorThriftFieldMetadata input) {		
		if(!PREALLOC_FILTER.apply(input)){
			if(!Requiredness.REQUIRED.equals(input.getRequiredness())){
				CodegenRequired ann = ThriftFieldMetadataUtil.extractFieldAnnotation(input,CodegenRequired.class);				
				if(ann == null || !ann.value()){
					switch (input.getErpcType().getProtocolType()) {
					case LIST:
					case STRING:
					case BINARY:
						return "";
					default:
						return "byref ";
					}
				}	
			}
			
		}
		return "";
	}

}
